﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace DiveManager
{
    public partial class MainForm : Form
    {
        protected DiveDatabaseEntities diveDatabase;

        public DateTime date=System.DateTime.Now;
        
        public MainForm()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();

            // Open Manage Activities
            ManageActivitiesForm manageActivitiesForm = new ManageActivitiesForm();
            manageActivitiesForm.TopLevel = false;
            ChangeForm(manageActivitiesForm);            
        }

        private void cmdEditCustomers_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Open ManageGuidesForm
            ManageCustomersForm managerCustomerForm = new ManageCustomersForm();
            managerCustomerForm.TopLevel = false;
            ChangeForm(managerCustomerForm);
        }

        private void cmdEditActivities_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Open ManageActivitiesForm
            ManageActivitiesForm manageActivitiesForm = new ManageActivitiesForm();
            manageActivitiesForm.TopLevel = false;
            ChangeForm(manageActivitiesForm);
        }

        private void cmdSync_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Open ManageActivitiesForm
            ConfigForm configForm = new ConfigForm();
            configForm.TopLevel = false;
            ChangeForm(configForm);
        }

        private void ChangeForm(Form form)
        {
            // Close old Form
            if (DataArea.Controls.Count == 1)
                ((Form)DataArea.Controls[0]).Close();

            // Add Form to DataArea
            DataArea.Controls.Clear();
            DataArea.Controls.Add(form);
            form.Dock = DockStyle.Fill;
            form.Show();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Close Child Form
            if (DataArea.Controls.Count == 1)
                ((Form)DataArea.Controls[0]).Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            timerCheckDB.Start();

            //Usernamen auslesen
            string username = Environment.GetEnvironmentVariable("username", EnvironmentVariableTarget.Process);

            //Systemanmeldung in ReportingSystem hinterlegen
            //Schreiben in Reporting
            ReportingMain report = new ReportingMain();
            report.ID = System.Guid.NewGuid();
            report.Report_Text = "Anmeldung an System";
            report.Sortierung = 3001;
            report.Objekt = username;
            report.Date = date;
            // Add to DB
            diveDatabase.AddToReportingMain(report);
            diveDatabase.SaveChanges();

            //Usernamen in den FormKopf schreiben

            this.Text = "DiveManager       User: " + username;  
        }

        private void check_DB_connection(object sender, EventArgs e)
        {
            DatabaseTools dbt = new DatabaseTools();

            //if (dbt.checkRemoteDatabase())
            //{
            //    statusRemoteDB.Image = global::DiveManager.Properties.Resources.greencircle;                
            //}
            //else
            //{
            //    statusRemoteDB.Image = global::DiveManager.Properties.Resources.redcircle;
            //}

            if (dbt.checkDatabase())
            {
                statusLocalDB.Image = global::DiveManager.Properties.Resources.greencircle;
            }
            else
            {
                statusLocalDB.Image = global::DiveManager.Properties.Resources.redcircle;
            }


        }

        private void linkLabel7_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Open MoneySupply
            MoneySupply moneysupply = new MoneySupply();
            moneysupply.TopLevel = false;
            ChangeForm(moneysupply);
        }

        private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Open ValuesAcc
            ValuesAcc valueaccounts = new ValuesAcc();
            valueaccounts.TopLevel = false;
            ChangeForm(valueaccounts);
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Open Journal
            Journal journal = new Journal();
            journal.TopLevel = false;
            ChangeForm(journal);
        }

        private void cmdSync_Click(object sender, EventArgs e)
        {
            // Open ManageActivitiesForm
            ConfigForm configForm = new ConfigForm();
            configForm.TopLevel = false;
            ChangeForm(configForm);
        }

        private void cmdEditUsers_Click(object sender, EventArgs e)
        {
            // Open ManageActivitiesForm
            ManageUsersForm manageUsersForm = new ManageUsersForm();
            manageUsersForm.TopLevel = false;
            ChangeForm(manageUsersForm);
        }

        private void cmdEditRights_Click(object sender, EventArgs e)
        {
            // Open ManageRightForm
            ManageRightsForm ManageRightsForm = new ManageRightsForm();
            ManageRightsForm.TopLevel = false;
            ChangeForm(ManageRightsForm);
        }

        private void cmdProjectStatus_Click(object sender, EventArgs e)
        {
            // Open ProjektStatus
            ProjektStatus prostat = new ProjektStatus();
            prostat.TopLevel = false;
            ChangeForm(prostat);
        }

        private void cmdProductGroups_Click(object sender, EventArgs e)
        {
            // Open MoneySupply
            ProductGroups productgroups = new ProductGroups();
            productgroups.TopLevel = false;
            ChangeForm(productgroups);
        }

        private void cmdAddArticle_Click(object sender, EventArgs e)
        {
            // Open AddProduct
            AddProduct addproduct = new AddProduct();
            addproduct.TopLevel = false;
            ChangeForm(addproduct);
        }

        private void cmdAddSupplier_Click(object sender, EventArgs e)
        {
            // Open Supplier
            Supplier supplier = new Supplier();
            supplier.TopLevel = false;
            ChangeForm(supplier);
        }

        private void cmdDocumentExplorer_Click(object sender, EventArgs e)
        {
            // Open DataExplorer
            DataExplorerUpload dataexplorerupload = new DataExplorerUpload();
            dataexplorerupload.TopLevel = false;
            ChangeForm(dataexplorerupload);
        }

        private void cmdEditGuides_Click(object sender, EventArgs e)
        {
            // Open ManageGuidesForm
            ManageGuidesForm manageGuidesForm = new ManageGuidesForm();
            manageGuidesForm.TopLevel = false;
            ChangeForm(manageGuidesForm);
        }

        private void cmdAddCourse_Click(object sender, EventArgs e)
        {
            // Open ValueAccounts
            AddCourse addcourse = new AddCourse();
            addcourse.TopLevel = false;
            ChangeForm(addcourse);
        }

        private void cmdCustomerAdd_Click(object sender, EventArgs e)
        {
            // Open CustomerAdd
            AddCustomer customeradd = new AddCustomer();
            customeradd.TopLevel = false;
            ChangeForm(customeradd);
        }

        private void bt_Rental_Artikel_Click(object sender, EventArgs e)
        {
            // Open Rental_Main
            Rental_Main rentalartikel = new Rental_Main();
            rentalartikel.TopLevel = false;
            ChangeForm(rentalartikel);
        }

        private void cmdStorage_Click(object sender, EventArgs e)
        {
            // Open Rental_Main
            Storage_Main storagemain = new Storage_Main();
            storagemain.TopLevel = false;
            ChangeForm(storagemain);
        }

        private void btLiveReporting_Click(object sender, EventArgs e)
        {
            // Open LiveReportingMain
            LiveReportingMain livereportingmain = new LiveReportingMain();
            livereportingmain.TopLevel = false;
            ChangeForm(livereportingmain);
        }

        private void btEinkauf_Click(object sender, EventArgs e)
        {
            // Open PurchasingMain
            //PurchasingMain PurchasingMain = new PurchasingMain();
            //PurchasingMain.TopLevel = false;
            //ChangeForm(PurchasingMain);
        }

        private void btAirFill_Click(object sender, EventArgs e)
        {
            // Open FillKompressorMain
            FillKompressorMain FillKompressorMain = new FillKompressorMain();
            FillKompressorMain.TopLevel = false;
            ChangeForm(FillKompressorMain);
        }

        private void DataArea_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btWarenkorb_Click(object sender, EventArgs e)
        {
            // Open Warenkorb
            Warenkorb Warenkorb = new Warenkorb();
            Warenkorb.TopLevel = false;
            ChangeForm(Warenkorb);
        }

        private void btFinanzEinstellungen_Click(object sender, EventArgs e)
        {
            // Open FinanzEinstellungen
            FinanzEinstellungen FinanzEinstellungen = new FinanzEinstellungen();
            FinanzEinstellungen.TopLevel = false;
            ChangeForm(FinanzEinstellungen);
        }

        private void btKeys_Click(object sender, EventArgs e)
        {
            // Open ModulKeyMain
            ModulKeyMain ModulKeyMain = new ModulKeyMain();
            ModulKeyMain.TopLevel = false;
            ChangeForm(ModulKeyMain);
        }

        private void statusLocalDB_Click(object sender, EventArgs e)
        {

        }
    }
}
